// pages/chart/chart.js
var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeChecked: 1,
    dateChecked: 1,
    bills: '',
    billTyps: '',
    all: 0,
    avg: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBillType("queryBillByWeekToType", 1001, "支出")
    this.getData("getBillByWeek", 1001, "支出")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.changeChecked(this.data.typeChecked, this.data.dateChecked)
  },

  typeChecked(e) {
    this.changeChecked(e.currentTarget.dataset.index, this.data.dateChecked)
  },

  dateChecked(e) {
    this.changeChecked(this.data.typeChecked, e.currentTarget.dataset.index)
  },

  changeChecked(t, d) {
    var userId = getApp().globalData.userId
    console.log(userId)
    this.setData({
      typeChecked: t,
      dateChecked: d
    })
    if (t == 1) {
      if (d == 1) {
        this.getData("getBillByWeek", userId, "支出")
        this.getBillType("queryBillByWeekToType", userId, "支出")

      } else if (d == 2) {
        this.queryBillByMonthTo("queryBillByMonthTo", userId, "支出", "2021", "6")
        this.getBillTypeByMonth("queryBillByMonthToType", userId, "支出", "2021", "6")

      } else {
        this.queryBillByYear("queryBillByYear", userId, "支出", "2021")
        this.getBillTypeByYear("queryBillByYearToType", userId, "支出", "2021")
      }
    } else {
      if (d == 1) {
        this.getData("getBillByWeek", userId, "收入")
        this.getBillType("queryBillByWeekToType", userId, "收入")

      } else if (d == 2) {
        this.queryBillByMonthTo("queryBillByMonthTo", userId, "收入", "2021", "6")
        this.getBillTypeByMonth("queryBillByWeekToType", userId, "收入", "2021", "6")

      } else {
        this.queryBillByYear("queryBillByYear", userId, "收入", "2021")
        this.getBillTypeByYear("queryBillByWeekToType", userId, "收入", "2021")
      }
    }
  },

  getData(path, userId, billType) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          bills: res.data
        })
        that.createCharts(res)
      }
    })

  },

  queryBillByMonthTo(path, userId, billType, year, month) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType,
        year: year,
        month: month
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          bills: res.data
        })
        that.createCharts(res)
      }
    })

  },

  queryBillByYear(path, userId, billType, year) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType,
        year: year,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          bills: res.data
        })
        that.createCharts(res)
      }
    })

  },

  createCharts(res) {
    new wxCharts({
      canvasId: 'columnCanvas',
      type: 'line',
      categories: res.data.days,
      series: [{
        data: res.data.money,
      }],
      yAxis: {
        disabled: true//是否绘制y轴
      },
      xAxis: {
        disableGrid: true,//是否绘制网格
        fontColor: '#FCC729',
      },
      dataItem: {
        color: '#FCC729'
      },
      width: wx.getSystemInfoSync().windowWidth,//宽
      height: 200,//高
      legend: false,//是否显示图表下方各类别的标识
      // dataLabel: true,
      // dataPointShape: true
    });
  },

  getBillType(path, userId, billType) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          billTyps: res.data
        })
        var all = 0
        var avg = 0
        for (var i = 0; i < res.data.length; i++) {
          all = res.data[i].money + all
        }
        avg = Math.round(all / 7)
        that.setData({
          all,
          avg
        })
      }
    })
  },

  getBillTypeByMonth(path, userId, billType, year, month) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType,
        year: year,
        month: month
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          billTyps: res.data
        })
        var all = 0
        var avg = 0
        for (var i = 0; i < res.data.length; i++) {
          all = res.data[i].money + all
        }
        avg = Math.round(all / 30)
        that.setData({
          all,
          avg
        })
      }
    })
  },

  getBillTypeByYear(path, userId, billType, year) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/' + path, //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        billType: billType,
        year: year
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          billTyps: res.data
        })
        var all = 0
        var avg = 0
        for (var i = 0; i < res.data.length; i++) {
          all = res.data[i].money + all
        }
        avg = Math.round(all / 12)
        that.setData({
          all,
          avg
        })
      }
    })
  }






})